package com.nuc.wxh.vaccine_appointment.vaccines.controller.admin;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuc.wxh.vaccine_appointment.base.exception.VaccineException;
import com.nuc.wxh.vaccine_appointment.base.result.R;
import com.nuc.wxh.vaccine_appointment.base.result.ResultCodeEnum;
import com.nuc.wxh.vaccine_appointment.base.util.MD5;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.AdminHospital;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.vo.AdminHospitalVo;
import com.nuc.wxh.vaccine_appointment.vaccines.service.AdminHospitalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 医院信息
 前端控制器
 * </p>
 *
 * @author 王鑫浩
 * @since 2021-03-10
 */
@Api(tags = "医院管理")
@CrossOrigin
@RestController
@RequestMapping("/admin/hospital")
public class AdminHospitalController {

    @Autowired
    private AdminHospitalService hospitalService;

    @ApiOperation(value = "根据ID删除医院",notes = "逻辑删除")
    @DeleteMapping("remove/{id}")
    public R removeById(@ApiParam(value="医院id",required=true)@PathVariable String id){
        boolean result =hospitalService.removeById(id);
        if(result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation(value="根据ID批量删除医院")
    @DeleteMapping("batch-remove")
    public R removeRows(@ApiParam(value = "医院ID" ,required = true) @RequestBody List<String> idList){
        boolean result =hospitalService.removeByIds(idList);
        if(result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("分页查询医院列表")
    @GetMapping("list/{page}/{limit}")
    public R listPage(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                      @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
                      @ApiParam(value = "查询对象条件") AdminHospitalVo hospitalVo) {
        Page<AdminHospital> pageParam = new Page<>(page, limit);
        Page<AdminHospital> AdminHospitalPage=hospitalService.selectPage(pageParam, hospitalVo);
        List<AdminHospital> records = AdminHospitalPage.getRecords();
        long total = AdminHospitalPage.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation("新增医院")
    @PostMapping("save")
    public R save(@ApiParam(value = "医院对象", required = true) @RequestBody AdminHospital hospital) {
        hospital.setPassword(MD5.encrypt(hospital.getPassword()));
        boolean result = hospitalService.save(hospital);
        if (result) {
            return R.ok().message("保存成功");
        } else {
            return R.error().message("保存失败");
        }
    }

    @ApiOperation("医院修改")
    @PutMapping("update")
    public R updateById(@ApiParam(value = "医院对象", required = true) @RequestBody AdminHospital hospital) {

        boolean result = hospitalService.updateById(hospital);
        if (result) {
            return R.ok().message("修改成功");
        } else {
            return R.error().message("修改失败");
        }
    }

    @ApiOperation("根据id获取医院信息")
    @GetMapping("get/{id}")
    public R getById(@ApiParam(value = "医院ID", required = true) @PathVariable String id) {
        AdminHospital hospital = hospitalService.getById(id);
        if (hospital != null) {
            return R.ok().data("item", hospital);
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("excel批量添加")
    @PostMapping("saveExcel")
    public R saveExcel(MultipartFile file){
        try {
            hospitalService.saveExcel(file,hospitalService);
        }catch (Exception e){
            return R.error().message("添加失败可能有重复项");
        }
        return R.ok().message("添加成功");
    }

}

